package com.adcc.ags.msgStore.healthcheck;

import com.adcc.ags.msgStore.entity.db.DBInfo;
import com.adcc.ags.msgStore.entity.db.DBState;
import com.adcc.ags.msgStore.managed.MessageForward;
import com.adcc.utility.log.Log;
import com.codahale.metrics.health.HealthCheck;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * DB健康检查
 */
public class DBHealthCheck extends HealthCheck {

    @Override
    protected Result check() throws Exception {
        try {
            DBInfo dbInfo = MessageForward.getInstance().getDBInfo();
            if (dbInfo.getState() == DBState.CONNECTED) {
                return Result.healthy(new ObjectMapper().writeValueAsString(dbInfo));
            } else {
                return Result.unhealthy(new ObjectMapper().writeValueAsString(dbInfo));
            }
        } catch (Exception ex) {
            Log.error(DBHealthCheck.class.getName(), "check() error", ex);
            return Result.unhealthy(ex.toString());
        }
    }
}
